﻿using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command;
using IntelligentAgriculture.WebAPI.Application.Command.DescendanttransferCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
	/// <summary>
	/// 后裔转种
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class DescendanttransferController : ControllerBase
	{
		/// <summary>
		/// 依赖注入
		/// </summary>
		private readonly IMediator _mediator;
		private readonly ILogger<DescendanttransferController> _logger;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator"></param>
		public DescendanttransferController(IMediator mediator, ILogger<DescendanttransferController> logger)
		{
			_mediator = mediator;
			_logger = logger;
		}
		/// <summary>
		/// 获取后裔转种列表
		/// </summary>
		/// <param name="conmand"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<PageOutput<DescendanttransferOutPut>>> GetDescendanttransfers(GetDescendanttransfersCommand conmand)
		{
			try
			{
				return Results<PageOutput<DescendanttransferOutPut>>.DataResult(await _mediator.Send(conmand));
			}
			catch (Exception ex)
			{
				_logger.LogError("获取后裔转种列表异常" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 删除后裔转种
		/// </summary>
		/// <param name="conmand"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> DelDescendanttransfer(params string[] eartag)
		{
			try
			{
				var command = new DelDescendanttransferCommand(eartag);
				return Results<int>.DataResult(await _mediator.Send(command));
			}
			catch (Exception ex)
			{
				_logger.LogError("获取后裔转种列表异常" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 添加后裔转种
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> AddDelDescendanttransfer(List<Descendanttransfer> entity)
		{
			try
			{
				var command = new AddDescendanttransferCommand(entity);
				return Results<int>.DataResult(await _mediator.Send(command));
			}
			catch (Exception ex)
			{
				_logger.LogError("添加后裔转种异常" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 反填后裔转种
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public async Task<Results<Descendanttransfer>> DetaileDescendanttransfer(string eartag)
		{
			try
			{

				var command = new DetaileDescendanttransferCommand(eartag);
				return Results<Descendanttransfer>.DataResult(await _mediator.Send(command));
			}
			catch (Exception ex)
			{
				_logger.LogError("后裔转种详情异常" + ex.Message);
				throw;
			}
		}
		/// <summary>
		/// 修改后裔转种
		/// </summary>
		/// <returns></returns>
		[HttpPut]
		public async Task<Results<int>> UpdateDescendanttransfer( UpdateDescendanttransferCommand command)
		{
			try
			{
				return Results<int>.DataResult(await _mediator.Send(command));
			}
			catch (Exception ex)
			{
				_logger.LogError("修改后裔转种异常" + ex.Message);
				throw;
			}
		}
	}
}
